package com.tjcu.filter;

import com.tjcu.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/11/4 17:08
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
         HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        User user = (User) request.getSession().getAttribute("user");
        if( user==null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
         }else {
             if(user.getRole()==0){
                 filterChain.doFilter(request,response);
             }else {

             }

         }

    }

    @Override
    public void destroy() {

    }
}
